<template>
  <div>
    <el-dialog
      :title="info.type == 'add' ? '新增' : '修改'"
      :visible.sync="info.isShow"
      width="60%"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="上级目录" prop="pid">
          <el-select v-model="ruleForm.pid">
            <el-option label="顶级目录" :value="0"></el-option>
            <el-option
              v-for="(item, index) in selectList"
              :key="index + 'option'"
              :label="item.title"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-radio-group :disabled="info.type == 'edit'" v-model="ruleForm.type">
            <el-radio :label="1">目录</el-radio>
            <el-radio :label="2">菜单</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="ruleForm.title"></el-input>
        </el-form-item>
        <el-form-item v-show="ruleForm.type === 2" label="地址" prop="url">
          <el-input v-model="ruleForm.url"></el-input>
        </el-form-item>
        <el-form-item v-show="ruleForm.type === 1" label="图标" prop="icon">
          <el-input v-model="ruleForm.icon"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="ruleForm.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="info.isShow = false">取 消</el-button>
        <el-button v-if="info.type == 'add'" type="primary" @click="submitForm"
          >新增</el-button
        >
        <el-button v-else type="primary" @click="submitForm">修改</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getMenuListTree, menuAdd, menuEdit } from "../../http";
export default {
  props: ["info"],
  data() {
    return {
      // 下拉菜单的数据
      selectList: [],
      ruleForm: {},
      rules: {
        title: [{ required: true, message: "请输入菜单标题", trigger: "blur" }],
      },
    };
  },
  created() {
    // 获取下拉菜单的数据
    this.getSelectList();
  },
  methods: {
    async getSelectList() {
      let res = await getMenuListTree();
      this.selectList = res.list;
    },
    setValue(val) {
      // 根据父组件传来的值（新增？修改？）给表单填入数据
      this.ruleForm = { ...val };
    },
    // 关闭弹窗
    // handleClose(done) {
    //   done();
    // },
    submitForm() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          // 通过校验 准备提交数据
          // console.log(this.ruleForm);
          let res;
          if (this.info.type === "add") {
            res = await menuAdd(this.ruleForm);
          } else if (this.info.type === "edit") {
            res = await menuEdit(this.ruleForm);
          }
          if (res.code === 200) {
            this.$message({
              type: "success",
              message: res.msg,
            });
            this.info.isShow = false;
            // 子传父 刷新list组件
            this.$emit("update");
          } else {
            console.log("error submit!!");
            return false;
          }
        }
      });
    },
  },
};
</script>

<style>
</style>